import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_frontend/environment_config.dart';
import 'package:flutter_frontend/pages/main/main_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:wakelock/wakelock.dart';

GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (Platform.isAndroid) {
      SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(statusBarColor: Colors.transparent));
    }
    return ScreenUtilInit(
      designSize: const Size(2388, 1668),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (BuildContext context, Widget? child) => MaterialApp(
        title: EnvironmentConfig.appName,
        navigatorKey: navigatorKey,
        theme: ThemeData.light(),
        localizationsDelegates: const <LocalizationsDelegate>[GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate],
        debugShowCheckedModeBanner: false,
        locale: const Locale('zh'),
        supportedLocales: const <Locale>[Locale('zh')],
        localeResolutionCallback: (Locale? locale, Iterable<Locale>? supportedLocales) => locale,
        home: const _MyHomePage(),
        builder: EasyLoading.init(),
      ),
    );
  }
}

class _MyHomePage extends StatefulWidget {
  const _MyHomePage({Key? key}) : super(key: key);

  @override
  State<_MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<_MyHomePage> with WidgetsBindingObserver {
  DateTime _lastPressedAt = DateTime.now();

  @override
  void initState() {
    super.initState();
    Wakelock.enable();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  Widget build(BuildContext context) {
    // final double shortestSide = MediaQuery.of(context).size.shortestSide;
    // if (shortestSide < 600) {
    //   return Scaffold(
    //     body: SafeArea(
    //       child: Center(
    //         child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
    //           Text('此App是Pad版本，请在Pad上安装运行', style: TextStyle(fontSize: 30.sp)),
    //           SizedBox(height: 24.w),
    //           ElevatedButton(
    //             style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(horizontal: 60, vertical: 15).w),
    //             onPressed: () => exit(0),
    //             child: const Text('关闭', style: TextStyle(color: Colors.white)),
    //           ),
    //         ]),
    //       ),
    //     ),
    //   );
    // }
    return Scaffold(
      body: WillPopScope(
        child: const MainPage(),
        onWillPop: () async {
          if (DateTime.now().difference(_lastPressedAt).inSeconds > 1) {
            _lastPressedAt = DateTime.now();
            EasyLoading.showToast('再点一次退出应用', toastPosition: EasyLoadingToastPosition.bottom);
            return false;
          }
          exit(0);
        },
      ),
    );
  }
}
